package com.jai.pms.service.impl;

import java.util.List;
import java.util.Objects;

import com.jai.pms.domain.Data;
import com.jai.pms.domain.Project;
import com.jai.pms.domain.ProjectData;
import com.jai.pms.mapper.ProjectMapper;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jai.pms.mapper.ProjectUserMapper;
import com.jai.pms.domain.ProjectUser;
import com.jai.pms.service.IProjectUserService;

/**
 * 项目人员Service业务层处理
 * 
 * @author v
 * @date 2023-10-12
 */
@Service
public class ProjectUserServiceImpl implements IProjectUserService 
{
    @Autowired
    private ProjectUserMapper projectUserMapper;

    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询项目人员
     * 
     * @param id 项目人员主键
     * @return 项目人员
     */
    @Override
    public ProjectUser selectProjectUserById(Long id)
    {
        return projectUserMapper.selectProjectUserById(id);
    }

    /**
     * 查询项目人员列表
     * 
     * @param projectUser 项目人员
     * @return 项目人员
     */
    @Override
    public List<ProjectUser> selectProjectUserList(ProjectUser projectUser)
    {
        return projectUserMapper.selectProjectUserList(projectUser);
    }

    /**
     * 新增项目人员
     * 
     * @param projectUser 项目人员
     * @return 结果
     */
    @Override
    public int insertProjectUser(ProjectUser projectUser)
    {
        preCheck(projectUser, true);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        projectUser.setCreateBy(loginUser.getUsername());
        projectUser.setCreateTime(DateUtils.getNowDate());
        return projectUserMapper.insertProjectUser(projectUser);
    }

    /**
     * 修改项目人员
     * 
     * @param projectUser 项目人员
     * @return 结果
     */
    @Override
    public int updateProjectUser(ProjectUser projectUser)
    {
        preCheck(projectUser, false);

        LoginUser loginUser = SecurityUtils.getLoginUser();
        projectUser.setUpdateBy(loginUser.getUsername());
        projectUser.setUpdateTime(DateUtils.getNowDate());
        return projectUserMapper.updateProjectUser(projectUser);
    }

    /**
     * 批量删除项目人员
     * 
     * @param ids 需要删除的项目人员主键
     * @return 结果
     */
    @Override
    public int deleteProjectUserByIds(Long[] ids)
    {
        return projectUserMapper.deleteProjectUserByIds(ids);
    }

    /**
     * 删除项目人员信息
     * 
     * @param id 项目人员主键
     * @return 结果
     */
    @Override
    public int deleteProjectUserById(Long id)
    {
        return projectUserMapper.deleteProjectUserById(id);
    }

    private ProjectUser preCheck(ProjectUser projectUser, boolean isAdd) {
        // 检查是否有重复信息，联合主键projectId和dataId
        ProjectUser query = new ProjectUser();
        query.setProjectId(projectUser.getProjectId());
        query.setUserId(projectUser.getUserId());
        List<ProjectUser> queryList = projectUserMapper.selectProjectUserList(query);
        if (isAdd) {
            if (queryList.size() > 0) {
                throw new ServiceException("该用户在项目中已经存在");
            }
        } else {
            if (queryList.size() > 0 && !Objects.equals(queryList.get(0).getId(), projectUser.getId())) {
                throw new ServiceException("该用户在项目中已经存在");
            }
        }

        // 填充project的code、name信息
        Project project = projectMapper.selectProjectById(projectUser.getProjectId());
        projectUser.setProjectCode(project.getCode());
        projectUser.setProjectName(project.getName());

        return projectUser;
    }
}
